package pl.webapps.alarmme;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity {

    private TimeUtils mTimeUtils;
    private TimePicker mTimePicker;
    private ListViewMapper mListViewMapper;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!LocalResourcesHandler.checkIfSdCardPresentAndHaveSpace()) {
            setContentView(R.layout.error_sdcard);
            return;
        }
        PreferencesHandler.setSharedPreferences(getPreferences(MODE_PRIVATE));
        mTimeUtils = new TimeUtils(getPreferences(MODE_PRIVATE));
        setContentView(R.layout.main);
        prepareViewElements();
    }

    private void prepareViewElements() {
        mTimePicker = (TimePicker) findViewById(R.id.time_picker);
        mTimePicker.setIs24HourView(true);
        Integer hh = mTimeUtils.getLastSavedHour();
        Integer mm = mTimeUtils.getLastSavedMinute();
        if (hh == null || mm == null || hh < 0 || mm < 0) {
            mTimePicker.setCurrentHour(new Integer(12));
            mTimePicker.setCurrentMinute(new Integer(0));
        } else {
            mTimePicker.setCurrentHour(hh);
            mTimePicker.setCurrentMinute(mm);
        }
        Button acc = (Button) findViewById(R.id.button_accept);
        Button rej = (Button) findViewById(R.id.button_decline);
        OnClickListener listener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.button_accept :
                        acceptButtonAction();
                        break;
                    case R.id.button_decline :
                        rejectButtonAction();
                        break;
                }
            }
        };
        acc.setOnClickListener(listener);
        rej.setOnClickListener(listener);
        mListViewMapper = new ListViewMapper((ListView) findViewById(R.id.alarms_list), this);
    }

    private void acceptButtonAction() {
        int result = mListViewMapper.addNewAlarm(mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute(), mTimePicker
                .is24HourView());
        switch (result) {
            case AlarmUtils.ERR_ALARM_EXISTS :
                Toast.makeText(this.getApplicationContext(), getResources().getString(R.string.toast_alarm_error_already_exists), Toast.LENGTH_LONG)
                        .show();
                break;
            case AlarmUtils.ERR_ALARM_LIMIT_REACHED :
                Toast.makeText(this.getApplicationContext(), getResources().getString(R.string.toast_alarm_error_limit_reached), Toast.LENGTH_LONG)
                        .show();
                break;
            case AlarmUtils.ERR_ALARM_UNKNOWN_ERROR :
                Toast.makeText(this.getApplicationContext(), getResources().getString(R.string.toast_alarm_error_unknown_error), Toast.LENGTH_LONG)
                        .show();
                break;
            case AlarmUtils.ALARM_ADDED_SUCCESSFULLY :
                mTimeUtils.saveLastAlarmTime(mTimePicker.getCurrentHour().intValue(), mTimePicker.getCurrentMinute().intValue());
                Toast.makeText(this.getApplicationContext(), getResources().getString(R.string.alarm_set), Toast.LENGTH_SHORT)
                        .show();
                break;
        }
    }

    private void rejectButtonAction() {
        this.finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.menu_options :
                startActivity(new Intent(this, OptionsActivity.class));
                return true;
            case R.id.menu_recording :
                startActivity(new Intent(this, RecordingActivity.class));
                return true;
            default :
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Msg.d("MainActivity.onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
    }
}
